package com.woniuxy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.*;
import com.woniuxy.service.OrdermanageService;
import com.woniuxy.service.RoomService;
import com.woniuxy.service.StaffevaService;
import com.woniuxy.service.StaffevasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Ordermanage)表控制层
 *
 * @author makejava
 * @since 2021-05-24 15:49:47
 */
@RestController
@RequestMapping("ordermanage")
public class OrdermanageController {
    /**
     * 服务对象
     */
    @Resource
    private OrdermanageService ordermanageService;
    @Resource
    private RoomService roomService;
    @Resource
    private StaffevaService staffevaService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Ordermanage selectOne(Integer id) {
        return this.ordermanageService.queryById(id);
    }

    /**@描述  获取所有的订单信息*/
    @GetMapping("selectAll")
    public PageInfo<Ordermanage> selectAll(int startpage,int pagesize){
        PageHelper.startPage(startpage,pagesize);
        Page<Ordermanage> page = (Page<Ordermanage>)this.ordermanageService.quaryAllOrder();
        PageInfo<Ordermanage> pageInfo = new PageInfo<>(page);
        return pageInfo;

    }


    /**@描述  模糊查询*/
    @PostMapping("search")
    public PageInfo<Ordermanage> search(@RequestBody OrdermanageDto ordermanageDto){
        System.out.println(ordermanageDto.toString());
        PageHelper.startPage(ordermanageDto.getPage(),ordermanageDto.getLimit());
        Page<Ordermanage> page = (Page<Ordermanage>)this.ordermanageService.queryAll(ordermanageDto.getOrdermanage());
        PageInfo<Ordermanage> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }


    /**@描述  增加*/
    @PostMapping("insert")
    public String insert(@RequestBody Ordermanage ordermanage){
        Ordermanage ordermanage1 = this.ordermanageService.insert(ordermanage);
        if (ordermanage1 != null){
            return "success";
        }
        return "failure";

    }

    /**@描述  更改*/
    @PostMapping("update")
    public String update(@RequestBody Ordermanage ordermanage){
        Ordermanage update = this.ordermanageService.update(ordermanage);
        if (update != null){
            return "success";
        }
        return "failure";
    }

    /**@描述  删除*/
    @PostMapping("delete/{orderId}")
    public String delete(@PathVariable("orderId") int orderId){
        boolean b = this.ordermanageService.deleteById(orderId);
        if (b){
            return "success";
        }
        return "failure";
    }



    /**
     * Auther: Hello Mr. Guo  <br/>
     * Date: 17:45:2021/5/24  <br/>
     * Description:新增大订单
     */
    @PostMapping("insertOrder")
    @ApiOperation("新增大订单")
    public ResponseEntity<Ordermanage> insertOrder(@RequestBody Ordermanage ordermanage){
        System.out.println("前端传送的数据"+ordermanage);
        //给订单添加编号（时间戳）
        //String time = new Date().getTime()+"";

        String time = System.currentTimeMillis()+"";
        ordermanage.setOrderNumber(time);

        //给大订单添加订单总额
        ordermanage.setOrderTotal(0.00);

        //给大订单添加结算金额
        ordermanage.setOrderAmoutMoney(0.00);

        //给大订单添加状态(2，子订单未完成，不能进行结算)
        ordermanage.setOrderStatus(2);

        //根据房间id查询房间编号
        int orderRoomid = ordermanage.getOrderRoomid();
        Room room = this.roomService.queryById(orderRoomid);
        String roomCode = room.getRoomCode();

        //改变房间使用人数
        room.setRoomUsernum(ordermanage.getOrderUsername());
        //改变房间状态
        room.setRoomStatus("使用");

        //改变数据库里的数据
        this.roomService.update(room);

        //给订单添加房间编号
        ordermanage.setOrderRoomnumber(roomCode);
        //往数据库添加订单数据
        Ordermanage insert = this.ordermanageService.insert(ordermanage);
        if (insert!=null){
            return new ResponseEntity(1,"新增大订单成功",insert);
        }
        return new ResponseEntity(0,"新增大订单失败");
    }


    /**
     * Auther: Hello Mr. Guo  <br/>
     * Date: 19:27:2021/5/24  <br/>
     * Description:更改大订单状态
     */
    @PutMapping("putOrder")
    @ApiOperation("订单支付")
    public ResponseEntity putOrder(@RequestBody Ordermanage ordermanage){
        System.out.println("订单支付对象" + ordermanage);
        //获取订单id
        Integer orderId = ordermanage.getOrderId();
        //根据订单id获取订单对象
        Ordermanage ordermanage1 = this.ordermanageService.queryById(orderId);
        //获取房间ID
        Integer roomid = ordermanage1.getOrderRoomid();
        //通过房间id获取房间对象
        Room room = this.roomService.queryById(roomid);
        //改变房间状态
        room.setRoomStatus("打扫");

        //增加订单评论
        //获取订单的相关信息
        List<Staffeva> list= this.staffevaService.selectordermessage();
        ArrayList<Staffeva> arrList=(ArrayList<Staffeva>)list;
        //System.out.println(arrList.size());
        for (int i=0;i<arrList.size();i++){
            Staffeva staffeva=arrList.get(i);
            staffeva.setStaffevaStar("5");
            staffeva.setStaffevaEva("非常好");
            staffeva.setStaffevaCustomer("顾客");
            staffeva.setStaffevaSfnm("否");
            this.staffevaService.insert(staffeva);
        }

        //改变房间使用人数
        room.setRoomUsernum(0);
        //更新数据库
        this.roomService.update(room);

        Ordermanage update = this.ordermanageService.update(ordermanage);
        if (update != null){


            return new ResponseEntity(1,"更改大订单状态成功");
        }
        return new ResponseEntity(0,"更改大订单状态失败");
    }

    /**
     * Auther: Hello Mr. Guo  <br/>
     * Date: 10:47:2021/5/25  <br/>
     * Description:父子订单两表查询
     */
    @PostMapping("selectAllOrder")
    @ApiOperation("获取订单")
        public ResponseEntity<PageInfo<Ordermanage>> selectAllOrder(@RequestBody OrdermanageDto ordermanageDto){
        System.out.println("前端获取订单的对象" + ordermanageDto);

        PageHelper.startPage(ordermanageDto.getPage(),ordermanageDto.getLimit());
        Page<Ordermanage> page = (Page<Ordermanage>)this.ordermanageService.selectAllOrder(ordermanageDto.getOrdermanage());
        PageInfo<Ordermanage> pageInfo = new PageInfo<>(page);
        if (pageInfo != null){
            return new ResponseEntity(1,"获取订单成功",pageInfo);
        }
        return new ResponseEntity(0,"获取订单失败");
    }

    /**
     * Auther: Hello Mr. Guo  <br/>
     * Date: 11:33:2021/5/25  <br/>
     * Description:获取订单详情
     */

    @PostMapping("selectOneOrder")
    @ApiOperation("订单详情")
    public ResponseEntity<Ordermanage> selectOneOrder(@RequestBody Ordermanage ordermanage){
        System.out.println("订单详情接口" + ordermanage);
        List<Ordermanage> ordermanages = this.ordermanageService.selectAllOrder(ordermanage);
        if (ordermanage!=null){
            return new ResponseEntity(1,"获取订单详情成功",ordermanages);
        }
        return new ResponseEntity(0,"获取订单详情成功");
    }

    /**通过房间id查询大订单的id,返回值是对象*/
    @GetMapping("selectordermanageid")
    @ApiOperation("通过房间id查订单id")
    public ResponseEntity<Ordermanage> selectordermanageidbyroomid(Integer orderRoomid){
        Ordermanage ordermanage=this.ordermanageService.selectordermanageidbyroomid(orderRoomid);
        if (ordermanage!=null){
            return new ResponseEntity(1,"通过房间id查订单id成功",ordermanage);
        }
        return new ResponseEntity(0,"通过房间id查订单id失败");
    }
}
